package com.atguigu.demo.customer0222.controller;


import com.atguigu.demo.customer0222.bean.Customer;
import com.atguigu.demo.customer0222.mapper.CustomerMapper;
import com.atguigu.demo.customer0222.service.CustomerService;
import com.atguigu.demo.customer0222.service.impl.CustomerServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController   //@Controller
public class CustomerController {

    @Autowired
    CustomerService customerService;



    @GetMapping("/sayhello")
    public String sayHello(){
        String a=null;
        System.out.println(a.toString());
        return "hello !";
    }

    @RequestMapping("/customer")
    public List<Customer> getCustomer(@RequestParam(value = "name",required = false) String name , @RequestParam(value = "age",required = false) Integer age ){
       // List<Customer> customerList= customerService.getCustomerByAge(age);

        List<Customer> customerList = customerService.list(new QueryWrapper<Customer>().eq(age!=null,"age", age).like(name!=null,"name",name));


        return  customerList ;
    }

    @RequestMapping("/customer/{id}")
    public String  getCustomerById( @PathVariable("id") String id){
        return " customer: id :"+id;
    }


    @PostMapping("/customer")
    public  String   saveCustomer( @RequestBody    Customer customer){
        //问题在哪？
        //    CustomerService customerService=new CustomerServiceImpl();
       //  customerService.saveCustomer(customer);
        customerService.saveOrUpdate(customer);
        return customer.toString();

    }



}
